#include "port.h"

char arr[4][1024];  // 定义一个 4x4 的字符数组
struct get_information datas;
float Hcsr04GetLength;

float string_to_float(const char *buf) {
    float sum = 0.0;
    char *token = strtok((char *)buf, " ");  // 以空格分隔字符串

    while (token != NULL) {
        float value = atof(token);
        sum += value;
        token = strtok(NULL, " ");
    }

    return sum;
}

int sever_to_client(int port, int *fd){
	*fd = socket(AF_INET, SOCK_STREAM, 0);
	if(*fd < 0){
		perror("socket");
		return 0;
	}
	
	int opt = 1;
    	if (setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
		perror("setsockopt");
		close(*fd);
		return -1;
    	}
	
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = inet_addr("192.168.100.98");
	//绑定
	if(bind(*fd, (struct sockaddr *)&addr, sizeof(addr))){
		perror("bind");
	}
	//设置监听，模式
	if(listen(*fd, 3)){
		perror("listen");
	}
	//接受请求
	struct sockaddr_in cli_addr;
	socklen_t len = sizeof(cli_addr);
	int fbap = accept(*fd, (struct sockaddr *)(&cli_addr), &len);
	if(fbap < 0){
		perror("accept");
	}
	printf("accept ok\n");
	return fbap;
}

int get_bufs() {
    int sfd = serial_open("/dev/ttyUSB0");
    if (sfd == -1) {
        perror("open");
        return -1;
    }
    printf("open device\n");
    if (serial_config(sfd) != 0) {
        perror("serial configure");
        serial_close(sfd);
        return -1;
    }
    char buf[1024];
	
    int i;
    while (1) {
        for(i = 0; i < 4; i++) {
            memset(buf, 0, sizeof(buf));
            int ret = serial_read_image(sfd, buf);
            if (ret == -1) {
                perror("read");
                printf("Error number: %d\n", errno);
                printf("Error string: %s\n", strerror(errno));
                printf("here\n");
                return -1;
            }
            //printf("arr[%d] = %s", i, buf);
            // 复制 buf 内容到 arr[i] 中
            if (buf[0] == '0') {
            	if(string_to_float(buf + 1) == 0){
            		continue;
            	}
            	printf("0\n");
                strncpy(datas.temperature, buf + 1, sizeof(datas.temperature) - 1);
                datas.temperature[sizeof(datas.temperature) - 1] = '\0';
                printf("datas.temperature = %s", datas.temperature);
            }
            if (buf[0] == '1') {
            	printf("1\n");
               	strncpy(datas.humidity, buf + 1, sizeof(datas.humidity) - 1);
                datas.humidity[sizeof(datas.humidity) - 1] = '\0';
                printf("datas.humidity = %s", datas.humidity);
            }
            if (buf[0] == '2') {
            	
                Hcsr04GetLength = string_to_float(buf + 1);
                printf("Hcsr04GetLength = %f\n", Hcsr04GetLength);
            }
            if (buf[0] == '3') {
            	printf("3\n");
                strncpy(datas.MQ_2, buf + 1, sizeof(datas.MQ_2) - 1);
                datas.MQ_2[sizeof(datas.MQ_2) - 1] = '\0';
                printf("datas.MQ_2 = %s", datas.MQ_2);
            }
        }
    }
}
int send_signal(){
	int sfd = serial_open("/dev/ttyUSB0");
    if (sfd == -1) {
        perror("open");
        return -1;
    }
    printf("open device\n");
    if (serial_config(sfd) != 0) {
        perror("serial configure");
        serial_close(sfd);
        return -1;
    }
    char buf[1024];
    serial_write(sfd,"open");

}

